import numpy as np
from sklearn import svm
def compute_proxy_distance(source_x,target_x,verbose=False):
    """
    Compute the proxy-a-distance of a source/target representation
    :param source_x:
    :param target_x:
    :param verbose:
    :return:
    """
    nb_source = np.shape(source_x)[0]
    nb_target = np.shape(target_x)[0]

    if verbose:
        print('PAD on',(nb_source,nb_target),'examples')

    C_list = np.logspace(-5,4,10)
    source_train = np.random.permutation(len(source_x))
    target_train = np.random.permutation(len(target_x))
    half_source, half_target = int(nb_source/10),int(nb_target/10)
    train_x = np.vstack((source_x[source_train[:half_source],:],target_x[target_train[:half_target],:]))
    train_y = np.hstack((np.zeros(half_source,dtype=int),np.ones(half_target,dtype=int)))
    test_x = np.vstack((source_x[source_train[half_source:],:],target_x[target_train[half_target:],:]))
    test_y = np.hstack((np.zeros(nb_source-half_source,dtype=int),np.ones(nb_target-half_target,dtype=int)))
    best_risk = 1.0
    for C in C_list:
        clf = svm.SVC(C=C,kernel='linear',verbose=False)
        clf.fit(train_x,train_y)
        train_risk = np.mean(clf.predict(train_x)!=train_y)
        test_risk = np.mean(clf.predict(test_x)!=test_y)

        if verbose:
            print('[ PAD C = %f ] train risk: %f test risk: %f'%(C,train_risk,test_risk))

        # if test_risk > .5:
        #     test_risk = 1. - test_risk

        best_risk = min(best_risk,test_risk)

    return 2*(1. - 2 * best_risk)

# np.random.seed(47)
np.random.seed(4)
#np.random.seed(8)
# print('==================================================================')
# Xs_origin = np.loadtxt("data/source10000.txt")
# Xt_origin = np.loadtxt("data/target10000.txt")
# print('Computing PAD on original data...')
# pad_original = compute_proxy_distance(Xs_origin, Xt_origin, verbose=True)
# print('PAD on original data = %f' % pad_original)
print('==================================================================')
Xs_jada = np.loadtxt("data/source-jan-aw500.txt")
Xt_jada = np.loadtxt("data/target--jan-aw500.txt")
print('Computing PAD on jan data...')
pad_jada = compute_proxy_distance(Xs_jada, Xt_jada, verbose=True)
print('PAD on jan data = %f' % pad_jada)
print('==================================================================')
Xs_mcd = np.loadtxt("data/source-resnet-aw500.txt")
Xt_mcd = np.loadtxt("data/target-resnet-aw500.txt")
print('Computing PAD on resnet data...')
pad_mcd = compute_proxy_distance(Xs_mcd, Xt_mcd, verbose=True)
print('PAD on resnet data = %f' % pad_mcd)
print('==================================================================')
# Xs_dann = np.loadtxt("/home/zb/桌面/sulimin/tsne_data_备份/DANN/source/source_20000_feature.txt")
# Xt_dann = np.loadtxt("/home/zb/桌面/sulimin/tsne_data_备份/DANN/target/target_20000_feature.txt")
# print('Computing PAD on DANN data...')
# pad_dann = compute_proxy_distance(Xs_dann, Xt_dann, verbose=True)
# print('PAD on DANN data = %f' % pad_dann)
# print('==================================================================')
# Xs_resnet = np.loadtxt("/home/zb/桌面/sulimin/tsne_data_备份/RESNET/source/source_20000_feature.txt")
# Xt_resnet = np.loadtxt("/home/zb/桌面/sulimin/tsne_data_备份/RESNET/target/target_20000_feature.txt")
# print('Computing PAD on RESNET data...')
# pad_resnet = compute_proxy_distance(Xs_resnet, Xt_resnet, verbose=True)
# print('PAD on RESNET data = %f' % pad_resnet)